package com.jxdinfo.hussar.example.outsourcereportandcertimanage.bgxzsq.wsdreportdownloadreq1.service.impl;

import com.jxdinfo.hussar.support.mp.base.service.impl.HussarServiceImpl;
import com.jxdinfo.hussar.support.datasource.annotations.HussarDs;
import com.jxdinfo.hussar.support.exception.HussarException;
import com.jxdinfo.hussar.platform.core.utils.HussarUtils;
import com.jxdinfo.hussar.example.outsourcereportandcertimanage.bgxzsq.wsdreportdownloadreq1.model.WsdReportDownloadReq1;
import com.jxdinfo.hussar.example.outsourcereportandcertimanage.bgxzsq.wsdreportdownloadreq1.service.WsdReportDownloadReq1Service;
import com.jxdinfo.hussar.example.outsourcereportandcertimanage.bgxzsq.wsdreportdownloadreq1.dao.WsdReportDownloadReq1Mapper;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jxdinfo.hussar.workflow.manage.engine.TaskEngineService;
import java.util.Arrays;
import com.jxdinfo.hussar.common.tfmodel.TfModelEnum;
import com.jxdinfo.hussar.common.security.BaseSecurityUtil;
import com.jxdinfo.hussar.support.transaction.core.annotation.HussarTransactional;
import com.jxdinfo.hussar.support.mp.base.query.generator.SingleTableQueryGenerator;
import com.jxdinfo.hussar.workflow.engine.response.BpmResponseResult;
import java.time.LocalDateTime;
import java.util.HashMap;
import com.jxdinfo.hussar.support.mp.base.query.generator.superquery.SuperQueryGenerator;
import com.jxdinfo.hussar.example.outsourcereportandcertimanage.bgxzsq.wsdreportdownloadreq1.dto.WsdReportDownloadReq1Dto;
import org.springframework.aop.framework.AopContext;
import java.util.ArrayList;
import com.jxdinfo.hussar.workflow.engine.constant.BpmConstant;
import java.util.Map;
import com.jxdinfo.hussar.support.mp.base.query.dto.QueryConditionDto;
import com.jxdinfo.hussar.example.outsourcereportandcertimanage.bgxzsq.wsdreportdownloadreq1.dto.WsdReportDownloadReq1SelectCondition;
import com.jxdinfo.hussar.workflow.manage.engine.InstanceEngineService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.jxdinfo.hussar.platform.core.base.apiresult.ApiResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jxdinfo.hussar.example.outsourcereportandcertimanage.bgxzsq.wsdreportdownloadreq1.vo.WsdReportDownloadReq1PageVO;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.jxdinfo.hussar.support.mp.base.query.dto.SuperQueryConditionDto;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 * 委外报告下载申请表1服务实现类
 */
@Service("outsourcereportandcertimanage.bgxzsq.wsdreportdownloadreq1.WsdReportDownloadReq1ServiceImpl")
@HussarDs("master")
public class WsdReportDownloadReq1ServiceImpl extends HussarServiceImpl<WsdReportDownloadReq1Mapper, WsdReportDownloadReq1> implements WsdReportDownloadReq1Service {

    private static final Logger logger = LoggerFactory.getLogger(WsdReportDownloadReq1ServiceImpl.class);

    private static final String RETURN_CODE = "0";

    @Override
    public ApiResponse<WsdReportDownloadReq1PageVO> ConditionFilterPage(WsdReportDownloadReq1SelectCondition wsdReportDownloadReq1SelectCondition) {
        try {
            Page<WsdReportDownloadReq1> pager = new Page<>(wsdReportDownloadReq1SelectCondition.getCurrent(), wsdReportDownloadReq1SelectCondition.getSize());
            Map<String, String[]> parameterMap = new HashMap<>();
            String[] orderParameter = new String[1];
            if (wsdReportDownloadReq1SelectCondition.getOrders() != null) {
                for (OrderItem orderItem : wsdReportDownloadReq1SelectCondition.getOrders()) {
                    if (orderItem.isAsc()) {
                        orderParameter[0] = orderItem.getColumn() + ",asc;";
                    } else {
                        orderParameter[0] = orderItem.getColumn() + ",desc;";
                    }
                }
            }
            parameterMap.put("orderRule", orderParameter);
            // 查询条件构造器,将排序条件封装成QueryWrapper
            SingleTableQueryGenerator singleTableQueryGenerator = new SingleTableQueryGenerator();
            QueryWrapper<WsdReportDownloadReq1> queryWrapper = singleTableQueryGenerator.initQueryWrapper(new WsdReportDownloadReq1(), parameterMap);
            if (HussarUtils.isNotEmpty(wsdReportDownloadReq1SelectCondition.getSuperQueryConditionDto())) {
                // 查询条件构造器,将查询条件封装成QueryWrapper
                List<SuperQueryConditionDto> superQueryConditionDtoList = wsdReportDownloadReq1SelectCondition.getSuperQueryConditionDto();
                SuperQueryGenerator<WsdReportDownloadReq1> superQueryGenerator = new SuperQueryGenerator(WsdReportDownloadReq1.class);
                superQueryGenerator.initSuperQueryWrapper(queryWrapper, superQueryConditionDtoList);
            }
            IPage<WsdReportDownloadReq1> list = this.page(pager, queryWrapper.lambda().and(wrapper -> wrapper.ne(WsdReportDownloadReq1::getDelFlag, "1").or().isNull(WsdReportDownloadReq1::getDelFlag)));
            WsdReportDownloadReq1PageVO wsdReportDownloadReq1PageVO = new WsdReportDownloadReq1PageVO();
            wsdReportDownloadReq1PageVO.setData(list.getRecords());
            wsdReportDownloadReq1PageVO.setCount(list.getTotal());
            wsdReportDownloadReq1PageVO.setCode(RETURN_CODE);
            return ApiResponse.success(wsdReportDownloadReq1PageVO);
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_FORM_QUERY_FAILED.getMessage());
        }
    }

    @Override
    public ApiResponse<WsdReportDownloadReq1PageVO> ConditionFilterPage_order_custom(WsdReportDownloadReq1SelectCondition wsdReportDownloadReq1SelectCondition) {
        try {
            Page<WsdReportDownloadReq1> pager = new Page<>(wsdReportDownloadReq1SelectCondition.getCurrent(), wsdReportDownloadReq1SelectCondition.getSize());
            Map<String, String[]> parameterMap = new HashMap<>();
            String[] orderParameter = new String[1];
            if (wsdReportDownloadReq1SelectCondition.getOrders() != null) {
                for (OrderItem orderItem : wsdReportDownloadReq1SelectCondition.getOrders()) {
                    if (orderItem.isAsc()) {
                        orderParameter[0] = orderItem.getColumn() + ",asc;";
                    } else {
                        orderParameter[0] = orderItem.getColumn() + ",desc;";
                    }
                }
            }
            parameterMap.put("orderRule", orderParameter);
            // 查询条件构造器,将排序条件封装成QueryWrapper
            SingleTableQueryGenerator singleTableQueryGenerator = new SingleTableQueryGenerator();
            QueryWrapper<WsdReportDownloadReq1> queryWrapper = singleTableQueryGenerator.initQueryWrapper(new WsdReportDownloadReq1(), parameterMap);
            if (HussarUtils.isNotEmpty(wsdReportDownloadReq1SelectCondition.getSuperQueryConditionDto())) {
                // 查询条件构造器,将查询条件封装成QueryWrapper
                List<SuperQueryConditionDto> superQueryConditionDtoList = wsdReportDownloadReq1SelectCondition.getSuperQueryConditionDto();
                SuperQueryGenerator<WsdReportDownloadReq1> superQueryGenerator = new SuperQueryGenerator(WsdReportDownloadReq1.class);
                superQueryGenerator.initSuperQueryWrapper(queryWrapper, superQueryConditionDtoList);
            }
            IPage<WsdReportDownloadReq1> list = this.page(pager, queryWrapper.lambda().and(wrapper -> wrapper.ne(WsdReportDownloadReq1::getDelFlag, "1").or().isNull(WsdReportDownloadReq1::getDelFlag)));
            WsdReportDownloadReq1PageVO wsdReportDownloadReq1PageVO = new WsdReportDownloadReq1PageVO();
            wsdReportDownloadReq1PageVO.setData(list.getRecords());
            wsdReportDownloadReq1PageVO.setCount(list.getTotal());
            wsdReportDownloadReq1PageVO.setCode(RETURN_CODE);
            return ApiResponse.success(wsdReportDownloadReq1PageVO);
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_FORM_QUERY_FAILED.getMessage());
        }
    }

    @Override
    @HussarTransactional
    @HussarDs("master")
    public ApiResponse<JSONObject> flowTableUpdate(WsdReportDownloadReq1Dto wsdReportDownloadReq1Dto) {
        try {
            return InstanceEngineService.validateEditAuthority(wsdReportDownloadReq1Dto.getUpdateRowId());
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_PROCESS_TABLE_IS_ABNORMALLY_MODIFIED.getMessage() + e.getMessage());
        }
    }

    @Override
    public ApiResponse<WsdReportDownloadReq1> formQuery(String id) {
        try {
            return ApiResponse.success(this.getById(id));
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_FORM_QUERY_FAILED.getMessage());
        }
    }

    @Override
    @HussarTransactional
    @HussarDs("master")
    public ApiResponse<String> endProcess(WsdReportDownloadReq1Dto wsdReportDownloadReq1Dto) {
        BpmResponseResult bpmResponseResult;
        try {
            if (HussarUtils.isEmpty(BaseSecurityUtil.getUser())) {
                throw new HussarException(TfModelEnum.TF_FAILED_T0_OBTAIN_USER_INFORMATION.getMessage());
            }
            String taskId = wsdReportDownloadReq1Dto.getTaskId();
            String comment = wsdReportDownloadReq1Dto.getComment();
            bpmResponseResult = InstanceEngineService.endProcess(taskId, String.valueOf(BaseSecurityUtil.getUser().getId()), comment);
            if (BpmConstant.SUCCESS_CODE.equals(bpmResponseResult.getCode())) {
                return ApiResponse.success("");
            }
            throw new HussarException(bpmResponseResult.getMsg());
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_PROCESS_TERMINATION_IS_ABNORMAL.getMessage() + e.getMessage());
        }
    }

    @Override
    @HussarTransactional
    public ApiResponse<String> initialNodeReject(WsdReportDownloadReq1Dto wsdReportDownloadReq1Dto) {
        // 基础数据模型
        WsdReportDownloadReq1 wsdReportDownloadReq1 = wsdReportDownloadReq1Dto.getFormdata();
        // 引用数据模型
        String taskId = wsdReportDownloadReq1Dto.getTaskId();
        String comment = wsdReportDownloadReq1Dto.getComment();
        try {
            if (wsdReportDownloadReq1.getBusinessId() == null || this.getById(wsdReportDownloadReq1.getBusinessId()) == null) {
                wsdReportDownloadReq1.setCreator(BaseSecurityUtil.getUser().getId());
                wsdReportDownloadReq1.setCreateTime(LocalDateTime.now());
                wsdReportDownloadReq1.setDelFlag("0");
                wsdReportDownloadReq1.setDataOrgan(BaseSecurityUtil.getUser().getDeptId());
            }
            this.saveOrUpdate(wsdReportDownloadReq1);
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_FAILED_TO_SAVE_FORM_DATA.getMessage());
        }
        return ((WsdReportDownloadReq1Service) AopContext.currentProxy()).initialnodereject(taskId, comment);
    }

    @Override
    @HussarTransactional
    @HussarDs("master")
    public ApiResponse<String> initialnodereject(String taskId, String comment) {
        BpmResponseResult bpmResponseResult;
        try {
            if (HussarUtils.isEmpty(BaseSecurityUtil.getUser())) {
                throw new HussarException(TfModelEnum.TF_FAILED_T0_OBTAIN_USER_INFORMATION.getMessage());
            }
            bpmResponseResult = TaskEngineService.rejectToFirstTask(taskId, String.valueOf(BaseSecurityUtil.getUser().getId()), comment, null, true, null);
            if (BpmConstant.SUCCESS_CODE.equals(bpmResponseResult.getCode())) {
                return ApiResponse.success("");
            }
            throw new HussarException(bpmResponseResult.getMsg());
        } catch (Exception e) {
            throw new HussarException(e.getMessage());
        }
    }

    @Override
    @HussarTransactional
    public ApiResponse<String> flowFormRecall(WsdReportDownloadReq1Dto wsdReportDownloadReq1Dto) {
        try {
            WsdReportDownloadReq1 wsdReportDownloadReq1 = wsdReportDownloadReq1Dto.getFormdata();
            String taskId = wsdReportDownloadReq1Dto.getTaskId();
            String comment = wsdReportDownloadReq1Dto.getComment();
            boolean isSubmit = wsdReportDownloadReq1Dto.isSubmit();
            if (wsdReportDownloadReq1.getBusinessId() == null || this.getById(wsdReportDownloadReq1.getBusinessId()) == null) {
                wsdReportDownloadReq1.setCreator(BaseSecurityUtil.getUser().getId());
                wsdReportDownloadReq1.setCreateTime(LocalDateTime.now());
                wsdReportDownloadReq1.setDelFlag("0");
                wsdReportDownloadReq1.setDataOrgan(BaseSecurityUtil.getUser().getDeptId());
            }
            this.saveOrUpdate(wsdReportDownloadReq1);
            ((WsdReportDownloadReq1Service) AopContext.currentProxy()).formrecall(taskId, comment, isSubmit);
            return ApiResponse.success();
        } catch (Exception e) {
            throw new HussarException(e.getMessage());
        }
    }

    @Override
    @HussarTransactional
    @HussarDs("master")
    public void formrecall(String taskId, String comment, boolean isSubmit) {
        if (HussarUtils.isEmpty(BaseSecurityUtil.getUser())) {
            throw new HussarException(TfModelEnum.TF_FAILED_T0_OBTAIN_USER_INFORMATION.getMessage());
        }
        String userId = String.valueOf(BaseSecurityUtil.getUser().getId());
        BpmResponseResult check;
        BpmResponseResult revoke;
        try {
            check = TaskEngineService.withdrawState(taskId);
            if (BpmConstant.ERROR_CODE.equals(check.getCode())) {
                throw new HussarException(check.getMsg());
            }
            if (!(Boolean) check.getResult().get(0)) {
                throw new HussarException(TfModelEnum.TF_THE_CURRENT_NODE_CANNOT_BE_RECALLED.getMessage());
            }
            revoke = TaskEngineService.revokeTask(taskId, userId, comment, isSubmit, null);
            if (BpmConstant.ERROR_CODE.equals(revoke.getCode())) {
                throw new HussarException(revoke.getMsg());
            }
        } catch (Exception e) {
            throw new HussarException(e.getMessage());
        }
    }

    @Override
    @HussarTransactional
    public ApiResponse<String> flowFormSubmitReject(WsdReportDownloadReq1Dto wsdReportDownloadReq1Dto) {
        WsdReportDownloadReq1 wsdReportDownloadReq1 = wsdReportDownloadReq1Dto.getFormdata();
        try {
            if (wsdReportDownloadReq1.getBusinessId() == null || this.getById(wsdReportDownloadReq1.getBusinessId()) == null) {
                wsdReportDownloadReq1.setCreator(BaseSecurityUtil.getUser().getId());
                wsdReportDownloadReq1.setCreateTime(LocalDateTime.now());
                wsdReportDownloadReq1.setDelFlag("0");
                wsdReportDownloadReq1.setDataOrgan(BaseSecurityUtil.getUser().getDeptId());
            }
            this.saveOrUpdate(wsdReportDownloadReq1);
            String returnCode = ((WsdReportDownloadReq1Service) AopContext.currentProxy()).formsubmitreject(wsdReportDownloadReq1Dto, String.valueOf(wsdReportDownloadReq1.getBusinessId()));
            if (BpmConstant.SUCCESS_CODE.equals(returnCode)) {
                return ApiResponse.success(TfModelEnum.TF_PROCESS_FORM_WAS_SUCCESSFULLY_SUBMITTED.getMessage());
            } else {
                throw new HussarException(TfModelEnum.TF_FAILED_TO_SUBMIT_THE_PROCESS_FORM.getMessage());
            }
        } catch (Exception e) {
            throw new HussarException(e.getMessage());
        }
    }

    @Override
    @HussarTransactional
    @HussarDs("master")
    public String formsubmitreject(WsdReportDownloadReq1Dto wsdReportDownloadReq1Dto, String businessKey) {
        if (HussarUtils.isEmpty(BaseSecurityUtil.getUser())) {
            throw new HussarException(TfModelEnum.TF_FAILED_T0_OBTAIN_USER_INFORMATION.getMessage());
        }
        Map<String, String> appointMap = new HashMap<>();
        Map<String, Object> variable = new HashMap<>();
        try {
            String taskId = wsdReportDownloadReq1Dto.getTaskId();
            String comment = wsdReportDownloadReq1Dto.getComment();
            if (wsdReportDownloadReq1Dto.getFlowSelect() == null) {
                appointMap.put(BpmConstant.STATIC_APPOINT_ASSIGNEE, wsdReportDownloadReq1Dto.getParticipantSelect());
            } else {
                appointMap.put(wsdReportDownloadReq1Dto.getFlowSelect(), wsdReportDownloadReq1Dto.getParticipantSelect());
                variable.put(BpmConstant.BPM_NEXT_NODE, wsdReportDownloadReq1Dto.getFlowSelect());
            }
            if (wsdReportDownloadReq1Dto.getSelectBranches() != null) {
                List<String> nextNodeIds = new ArrayList<>();
                for (Map<String, String> map : wsdReportDownloadReq1Dto.getSelectBranches()) {
                    appointMap.put(map.get("flowSelect"), map.get("participants"));
                    nextNodeIds.add(map.get("flowSelect"));
                }
                variable.put(BpmConstant.BPM_NEXT_NODE, String.join(",", nextNodeIds));
            }
            // 判断是否设置会签
            if (wsdReportDownloadReq1Dto.getMultiType() != null) {
                variable.put(BpmConstant.MULTI_TYPE, wsdReportDownloadReq1Dto.getMultiType());
                variable.put(BpmConstant.MULTI_FINISH_CONDITION, wsdReportDownloadReq1Dto.getMultiFinishCondition());
            }
            String processDefinitionKey = wsdReportDownloadReq1Dto.getProcessDefinitionKey();
            String userId = String.valueOf(BaseSecurityUtil.getUser().getId());
            if (HussarUtils.isEmpty(taskId)) {
                BpmResponseResult startResult;
                startResult = InstanceEngineService.startProcessInstanceByKey(processDefinitionKey, userId, businessKey, null);
                if (BpmConstant.SUCCESS_CODE.equals(startResult.getCode())) {
                    taskId = (String) startResult.getResult().getJSONObject(0).get("taskId");
                } else {
                    throw new HussarException(TfModelEnum.TF_FAILED_TO_CREATE_A_START_NODE.getMessage());
                }
            }
            BpmResponseResult bpmResponseResult = TaskEngineService.completeLeapTask(taskId, userId, appointMap, null, comment, variable);
            return bpmResponseResult.getCode();
        } catch (Exception e) {
            throw new HussarException(e.getMessage());
        }
    }

    @Override
    @HussarTransactional
    public ApiResponse<String> flowFormSaveStart(WsdReportDownloadReq1Dto wsdReportDownloadReq1Dto) {
        try {
            WsdReportDownloadReq1 wsdReportDownloadReq1 = wsdReportDownloadReq1Dto.getFormdata();
            String processDefinitionKey = wsdReportDownloadReq1Dto.getProcessDefinitionKey() == null ? "" : wsdReportDownloadReq1Dto.getProcessDefinitionKey();
            if (wsdReportDownloadReq1.getBusinessId() == null || this.getById(wsdReportDownloadReq1.getBusinessId()) == null) {
                wsdReportDownloadReq1.setCreator(BaseSecurityUtil.getUser().getId());
                wsdReportDownloadReq1.setCreateTime(LocalDateTime.now());
                wsdReportDownloadReq1.setDelFlag("0");
                wsdReportDownloadReq1.setDataOrgan(BaseSecurityUtil.getUser().getDeptId());
            }
            this.saveOrUpdate(wsdReportDownloadReq1);
            String businessKey = String.valueOf(wsdReportDownloadReq1.getBusinessId());
            ApiResponse<?> flowApiResponse = ((WsdReportDownloadReq1Service) AopContext.currentProxy()).startSaveProcessInstance(processDefinitionKey, businessKey);
            if (flowApiResponse.isSuccess()) {
                return ApiResponse.success(String.valueOf(wsdReportDownloadReq1.getBusinessId()), "");
            }
            throw new HussarException(flowApiResponse.getMsg());
        } catch (Exception e) {
            throw new HussarException(e.getMessage());
        }
    }

    @Override
    @HussarTransactional
    @HussarDs("master")
    public ApiResponse<String> startSaveProcessInstance(String processDefinitionKey, String businessKey) {
        BpmResponseResult isstartResult;
        try {
            isstartResult = InstanceEngineService.checkProcessInstByBusinessKey(businessKey);
            if (BpmConstant.SUCCESS_CODE.equals(isstartResult.getCode())) {
                if (HussarUtils.isEmpty(BaseSecurityUtil.getUser())) {
                    throw new HussarException(TfModelEnum.TF_FAILED_T0_OBTAIN_USER_INFORMATION.getMessage());
                }
                String userId = String.valueOf(BaseSecurityUtil.getUser().getId());
                BpmResponseResult startResult;
                startResult = InstanceEngineService.startProcessInstanceByKey(processDefinitionKey, userId, businessKey, null);
                if (BpmConstant.SUCCESS_CODE.equals(startResult.getCode())) {
                    return ApiResponse.success(startResult.getMsg());
                } else {
                    throw new HussarException(startResult.getMsg());
                }
            } else {
                return ApiResponse.success("");
            }
        } catch (Exception e) {
            throw new HussarException(e.getMessage());
        }
    }

    @Override
    public ApiResponse<WsdReportDownloadReq1PageVO> ConditionFilterwsdReportDownloadReq1Sort_1Page(WsdReportDownloadReq1SelectCondition wsdReportDownloadReq1SelectCondition) {
        try {
            Page<WsdReportDownloadReq1> pager = new Page<>(wsdReportDownloadReq1SelectCondition.getCurrent(), wsdReportDownloadReq1SelectCondition.getSize());
            Map<String, String[]> parameterMap = new HashMap<>();
            String[] orderParameter = new String[1];
            if (wsdReportDownloadReq1SelectCondition.getOrders() != null) {
                for (OrderItem orderItem : wsdReportDownloadReq1SelectCondition.getOrders()) {
                    if (orderItem.isAsc()) {
                        orderParameter[0] = orderItem.getColumn() + ",asc;";
                    } else {
                        orderParameter[0] = orderItem.getColumn() + ",desc;";
                    }
                }
            }
            parameterMap.put("orderRule", orderParameter);
            String[] defaultOrderParameter = new String[1];
            String defaultOrder = "";
            defaultOrder += "requestTime" + ",desc;";
            defaultOrderParameter[0] = defaultOrder;
            parameterMap.put("defaultOrderRule", defaultOrderParameter);
            // 查询条件构造器,将排序条件封装成QueryWrapper
            SingleTableQueryGenerator singleTableQueryGenerator = new SingleTableQueryGenerator();
            QueryWrapper<WsdReportDownloadReq1> queryWrapper = singleTableQueryGenerator.initQueryWrapper(new WsdReportDownloadReq1(), parameterMap);
            if (HussarUtils.isNotEmpty(wsdReportDownloadReq1SelectCondition.getSuperQueryConditionDto())) {
                // 查询条件构造器,将查询条件封装成QueryWrapper
                List<SuperQueryConditionDto> superQueryConditionDtoList = wsdReportDownloadReq1SelectCondition.getSuperQueryConditionDto();
                SuperQueryGenerator<WsdReportDownloadReq1> superQueryGenerator = new SuperQueryGenerator(WsdReportDownloadReq1.class);
                superQueryGenerator.initSuperQueryWrapper(queryWrapper, superQueryConditionDtoList);
            }
            IPage<WsdReportDownloadReq1> list = this.page(pager, queryWrapper.lambda().and(wrapper -> wrapper.ne(WsdReportDownloadReq1::getDelFlag, "1").or().isNull(WsdReportDownloadReq1::getDelFlag)));
            WsdReportDownloadReq1PageVO wsdReportDownloadReq1PageVO = new WsdReportDownloadReq1PageVO();
            wsdReportDownloadReq1PageVO.setData(list.getRecords());
            wsdReportDownloadReq1PageVO.setCount(list.getTotal());
            wsdReportDownloadReq1PageVO.setCode(RETURN_CODE);
            return ApiResponse.success(wsdReportDownloadReq1PageVO);
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_FORM_QUERY_FAILED.getMessage());
        }
    }

    @Override
    public ApiResponse<WsdReportDownloadReq1PageVO> ConditionFilterwsdReportDownloadReq1Sort_1Page_order_custom(WsdReportDownloadReq1SelectCondition wsdReportDownloadReq1SelectCondition) {
        try {
            Page<WsdReportDownloadReq1> pager = new Page<>(wsdReportDownloadReq1SelectCondition.getCurrent(), wsdReportDownloadReq1SelectCondition.getSize());
            Map<String, String[]> parameterMap = new HashMap<>();
            String[] orderParameter = new String[1];
            if (wsdReportDownloadReq1SelectCondition.getOrders() != null) {
                for (OrderItem orderItem : wsdReportDownloadReq1SelectCondition.getOrders()) {
                    if (orderItem.isAsc()) {
                        orderParameter[0] = orderItem.getColumn() + ",asc;";
                    } else {
                        orderParameter[0] = orderItem.getColumn() + ",desc;";
                    }
                }
            }
            parameterMap.put("orderRule", orderParameter);
            String[] defaultOrderParameter = new String[1];
            String defaultOrder = "";
            defaultOrder += "requestTime" + ",desc;";
            defaultOrderParameter[0] = defaultOrder;
            parameterMap.put("defaultOrderRule", defaultOrderParameter);
            // 查询条件构造器,将排序条件封装成QueryWrapper
            SingleTableQueryGenerator singleTableQueryGenerator = new SingleTableQueryGenerator();
            QueryWrapper<WsdReportDownloadReq1> queryWrapper = singleTableQueryGenerator.initQueryWrapper(new WsdReportDownloadReq1(), parameterMap);
            if (HussarUtils.isNotEmpty(wsdReportDownloadReq1SelectCondition.getSuperQueryConditionDto())) {
                // 查询条件构造器,将查询条件封装成QueryWrapper
                List<SuperQueryConditionDto> superQueryConditionDtoList = wsdReportDownloadReq1SelectCondition.getSuperQueryConditionDto();
                SuperQueryGenerator<WsdReportDownloadReq1> superQueryGenerator = new SuperQueryGenerator(WsdReportDownloadReq1.class);
                superQueryGenerator.initSuperQueryWrapper(queryWrapper, superQueryConditionDtoList);
            }
            IPage<WsdReportDownloadReq1> list = this.page(pager, queryWrapper.lambda().and(wrapper -> wrapper.ne(WsdReportDownloadReq1::getDelFlag, "1").or().isNull(WsdReportDownloadReq1::getDelFlag)));
            WsdReportDownloadReq1PageVO wsdReportDownloadReq1PageVO = new WsdReportDownloadReq1PageVO();
            wsdReportDownloadReq1PageVO.setData(list.getRecords());
            wsdReportDownloadReq1PageVO.setCount(list.getTotal());
            wsdReportDownloadReq1PageVO.setCode(RETURN_CODE);
            return ApiResponse.success(wsdReportDownloadReq1PageVO);
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_FORM_QUERY_FAILED.getMessage());
        }
    }

    @Override
    public ApiResponse<WsdReportDownloadReq1PageVO> ConditionFilterwsdReportDownloadReq1Condition_1wsdReportDownloadReq1Sort_1Page(WsdReportDownloadReq1SelectCondition wsdReportDownloadReq1SelectCondition) {
        try {
            Page<WsdReportDownloadReq1> pager = new Page<>(wsdReportDownloadReq1SelectCondition.getCurrent(), wsdReportDownloadReq1SelectCondition.getSize());
            Map<String, String[]> parameterMap = new HashMap<>();
            String[] orderParameter = new String[1];
            if (wsdReportDownloadReq1SelectCondition.getOrders() != null) {
                for (OrderItem orderItem : wsdReportDownloadReq1SelectCondition.getOrders()) {
                    if (orderItem.isAsc()) {
                        orderParameter[0] = orderItem.getColumn() + ",asc;";
                    } else {
                        orderParameter[0] = orderItem.getColumn() + ",desc;";
                    }
                }
            }
            parameterMap.put("orderRule", orderParameter);
            String[] defaultOrderParameter = new String[1];
            String defaultOrder = "";
            defaultOrder += "requestTime" + ",desc;";
            defaultOrderParameter[0] = defaultOrder;
            parameterMap.put("defaultOrderRule", defaultOrderParameter);
            // 查询条件构造器,将排序条件封装成QueryWrapper
            SingleTableQueryGenerator singleTableQueryGenerator = new SingleTableQueryGenerator();
            QueryWrapper<WsdReportDownloadReq1> queryWrapper = singleTableQueryGenerator.initQueryWrapper(new WsdReportDownloadReq1(), parameterMap);
            if (HussarUtils.isNotEmpty(wsdReportDownloadReq1SelectCondition.getSuperQueryConditionDto())) {
                // 查询条件构造器,将查询条件封装成QueryWrapper
                List<SuperQueryConditionDto> superQueryConditionDtoList = wsdReportDownloadReq1SelectCondition.getSuperQueryConditionDto();
                SuperQueryGenerator<WsdReportDownloadReq1> superQueryGenerator = new SuperQueryGenerator(WsdReportDownloadReq1.class);
                superQueryGenerator.initSuperQueryWrapper(queryWrapper, superQueryConditionDtoList);
            }
            IPage<WsdReportDownloadReq1> list = this.page(pager, queryWrapper.lambda().and(wrapper -> wrapper.ne(WsdReportDownloadReq1::getDelFlag, "1").or().isNull(WsdReportDownloadReq1::getDelFlag)));
            WsdReportDownloadReq1PageVO wsdReportDownloadReq1PageVO = new WsdReportDownloadReq1PageVO();
            wsdReportDownloadReq1PageVO.setData(list.getRecords());
            wsdReportDownloadReq1PageVO.setCount(list.getTotal());
            wsdReportDownloadReq1PageVO.setCode(RETURN_CODE);
            return ApiResponse.success(wsdReportDownloadReq1PageVO);
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_FORM_QUERY_FAILED.getMessage());
        }
    }

    @Override
    public ApiResponse<WsdReportDownloadReq1PageVO> ConditionFilterwsdReportDownloadReq1Condition_1wsdReportDownloadReq1Sort_1Page_order_custom(WsdReportDownloadReq1SelectCondition wsdReportDownloadReq1SelectCondition) {
        try {
            Page<WsdReportDownloadReq1> pager = new Page<>(wsdReportDownloadReq1SelectCondition.getCurrent(), wsdReportDownloadReq1SelectCondition.getSize());
            Map<String, String[]> parameterMap = new HashMap<>();
            String[] orderParameter = new String[1];
            if (wsdReportDownloadReq1SelectCondition.getOrders() != null) {
                for (OrderItem orderItem : wsdReportDownloadReq1SelectCondition.getOrders()) {
                    if (orderItem.isAsc()) {
                        orderParameter[0] = orderItem.getColumn() + ",asc;";
                    } else {
                        orderParameter[0] = orderItem.getColumn() + ",desc;";
                    }
                }
            }
            parameterMap.put("orderRule", orderParameter);
            String[] defaultOrderParameter = new String[1];
            String defaultOrder = "";
            defaultOrder += "requestTime" + ",desc;";
            defaultOrderParameter[0] = defaultOrder;
            parameterMap.put("defaultOrderRule", defaultOrderParameter);
            // 查询条件构造器,将排序条件封装成QueryWrapper
            SingleTableQueryGenerator singleTableQueryGenerator = new SingleTableQueryGenerator();
            QueryWrapper<WsdReportDownloadReq1> queryWrapper = singleTableQueryGenerator.initQueryWrapper(new WsdReportDownloadReq1(), parameterMap);
            if (HussarUtils.isNotEmpty(wsdReportDownloadReq1SelectCondition.getSuperQueryConditionDto())) {
                // 查询条件构造器,将查询条件封装成QueryWrapper
                List<SuperQueryConditionDto> superQueryConditionDtoList = wsdReportDownloadReq1SelectCondition.getSuperQueryConditionDto();
                SuperQueryGenerator<WsdReportDownloadReq1> superQueryGenerator = new SuperQueryGenerator(WsdReportDownloadReq1.class);
                superQueryGenerator.initSuperQueryWrapper(queryWrapper, superQueryConditionDtoList);
            }
            IPage<WsdReportDownloadReq1> list = this.page(pager, queryWrapper.lambda().and(wrapper -> wrapper.ne(WsdReportDownloadReq1::getDelFlag, "1").or().isNull(WsdReportDownloadReq1::getDelFlag)));
            WsdReportDownloadReq1PageVO wsdReportDownloadReq1PageVO = new WsdReportDownloadReq1PageVO();
            wsdReportDownloadReq1PageVO.setData(list.getRecords());
            wsdReportDownloadReq1PageVO.setCount(list.getTotal());
            wsdReportDownloadReq1PageVO.setCode(RETURN_CODE);
            return ApiResponse.success(wsdReportDownloadReq1PageVO);
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_FORM_QUERY_FAILED.getMessage());
        }
    }

    @Override
    @HussarTransactional
    public ApiResponse<String> prevNodeReject(WsdReportDownloadReq1Dto wsdReportDownloadReq1Dto) {
        try {
            WsdReportDownloadReq1 wsdReportDownloadReq1 = wsdReportDownloadReq1Dto.getFormdata();
            String taskId = wsdReportDownloadReq1Dto.getTaskId() == null ? "" : wsdReportDownloadReq1Dto.getTaskId();
            String comment = wsdReportDownloadReq1Dto.getComment() == null ? "" : wsdReportDownloadReq1Dto.getComment();
            if (wsdReportDownloadReq1.getBusinessId() == null || this.getById(wsdReportDownloadReq1.getBusinessId()) == null) {
                wsdReportDownloadReq1.setCreator(BaseSecurityUtil.getUser().getId());
                wsdReportDownloadReq1.setCreateTime(LocalDateTime.now());
                wsdReportDownloadReq1.setDelFlag("0");
                wsdReportDownloadReq1.setDataOrgan(BaseSecurityUtil.getUser().getDeptId());
            }
            this.saveOrUpdate(wsdReportDownloadReq1);
            return ((WsdReportDownloadReq1Service) AopContext.currentProxy()).startAnyNodeRejectInstance(taskId, comment);
        } catch (Exception e) {
            throw new HussarException(e.getMessage());
        }
    }

    @Override
    @HussarTransactional
    @HussarDs("master")
    public ApiResponse<String> startAnyNodeRejectInstance(String taskId, String comment) {
        if (HussarUtils.isEmpty(BaseSecurityUtil.getUser())) {
            throw new HussarException(TfModelEnum.TF_FAILED_T0_OBTAIN_USER_INFORMATION.getMessage());
        }
        String userId = String.valueOf(BaseSecurityUtil.getUser().getId());
        BpmResponseResult bpmResponseResult;
        try {
            bpmResponseResult = TaskEngineService.rejectToLastTask(taskId, userId, comment, null, true, null);
            if (BpmConstant.SUCCESS_CODE.equals(bpmResponseResult.getCode())) {
                return ApiResponse.success("");
            }
            throw new HussarException(bpmResponseResult.getMsg());
        } catch (Exception e) {
            throw new HussarException(e.getMessage());
        }
    }

    @Override
    @HussarTransactional
    public ApiResponse<String> flowFormSubmit(WsdReportDownloadReq1Dto wsdReportDownloadReq1Dto) {
        WsdReportDownloadReq1 wsdReportDownloadReq1 = wsdReportDownloadReq1Dto.getFormdata();
        try {
            if (wsdReportDownloadReq1.getBusinessId() == null || this.getById(wsdReportDownloadReq1.getBusinessId()) == null) {
                wsdReportDownloadReq1.setCreator(BaseSecurityUtil.getUser().getId());
                wsdReportDownloadReq1.setCreateTime(LocalDateTime.now());
                wsdReportDownloadReq1.setDelFlag("0");
                wsdReportDownloadReq1.setDataOrgan(BaseSecurityUtil.getUser().getDeptId());
            }
            this.saveOrUpdate(wsdReportDownloadReq1);
        } catch (Exception e) {
            throw new HussarException(e.getMessage());
        }
        ((WsdReportDownloadReq1Service) AopContext.currentProxy()).formsubmit(wsdReportDownloadReq1Dto, String.valueOf(wsdReportDownloadReq1.getBusinessId()));
        return ApiResponse.success(String.valueOf(wsdReportDownloadReq1.getBusinessId()), TfModelEnum.TF_PROCESS_FORM_WAS_SUCCESSFULLY_SUBMITTED.getMessage());
    }

    @Override
    @HussarTransactional
    @HussarDs("master")
    public void formsubmit(WsdReportDownloadReq1Dto wsdReportDownloadReq1Dto, String businessKey) {
        if (HussarUtils.isEmpty(BaseSecurityUtil.getUser())) {
            throw new HussarException(TfModelEnum.TF_FAILED_T0_OBTAIN_USER_INFORMATION.getMessage());
        }
        Map<String, String> appointMap = new HashMap<>();
        Map<String, Object> variable = new HashMap<>();
        String taskId = wsdReportDownloadReq1Dto.getTaskId();
        String comment = wsdReportDownloadReq1Dto.getComment();
        if (wsdReportDownloadReq1Dto.getFlowSelect() == null) {
            appointMap.put(BpmConstant.STATIC_APPOINT_ASSIGNEE, wsdReportDownloadReq1Dto.getParticipantSelect());
        } else {
            appointMap.put(wsdReportDownloadReq1Dto.getFlowSelect(), wsdReportDownloadReq1Dto.getParticipantSelect());
            variable.put(BpmConstant.BPM_NEXT_NODE, wsdReportDownloadReq1Dto.getFlowSelect());
        }
        if (wsdReportDownloadReq1Dto.getSelectBranches() != null) {
            List<String> nextNodeIds = new ArrayList<>();
            for (Map<String, String> map : wsdReportDownloadReq1Dto.getSelectBranches()) {
                appointMap.put(map.get("flowSelect"), map.get("participants"));
                nextNodeIds.add(map.get("flowSelect"));
            }
            variable.put(BpmConstant.BPM_NEXT_NODE, String.join(",", nextNodeIds));
        }
        // 判断是否设置会签
        if (wsdReportDownloadReq1Dto.getMultiType() != null) {
            variable.put(BpmConstant.MULTI_TYPE, wsdReportDownloadReq1Dto.getMultiType());
            variable.put(BpmConstant.MULTI_FINISH_CONDITION, wsdReportDownloadReq1Dto.getMultiFinishCondition());
        }
        if (wsdReportDownloadReq1Dto.getNodeParticipantSelect() != null) {
            variable.put(BpmConstant.PUBLIC_APPOINT_ASSIGNEE, wsdReportDownloadReq1Dto.getNodeParticipantSelect());
        }
        String processDefinitionKey = wsdReportDownloadReq1Dto.getProcessDefinitionKey();
        String userId = String.valueOf(BaseSecurityUtil.getUser().getId());
        if (HussarUtils.isEmpty(taskId)) {
            BpmResponseResult startResult = InstanceEngineService.startProcessInstanceByKey(processDefinitionKey, userId, businessKey, variable);
            if (BpmConstant.SUCCESS_CODE.equals(startResult.getCode())) {
                taskId = (String) startResult.getResult().getJSONObject(0).get("taskId");
            } else {
                throw new HussarException(startResult.getMsg());
            }
        }
        BpmResponseResult bpmResponseResult = TaskEngineService.completeTask(taskId, userId, appointMap, null, comment, variable);
        if (!BpmConstant.SUCCESS_CODE.equals(bpmResponseResult.getCode())) {
            throw new HussarException(bpmResponseResult.getMsg());
        }
    }
}
